/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.phoenix.repository.dao;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.SQLException;


/**
 * Jigsaw server
 * 
 * @author    <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date      2012-11-15
 * @version   1.0.0
 */
public class NodeTableDeleteDao  extends JdbcDao {

	/**
	 * 删除结点（type=0或1 都删除）
	 * 
	 * @param ID
	 *            String
	 * @throws SQLException
	 */
	private static final String SQL_DELETE_BY_ID = "delete from NODE where PKID=?";

	public void delete(String ID) throws SQLException {
		PreparedStatement command = null;
		try {
			command = connection.prepareStatement(SQL_DELETE_BY_ID);
			command.setString(1, ID);
			command.executeUpdate();
		} finally {
			this.close(command);
		}
	}
	/**
	 * 删除node，同时删除相关所有扩展属性的值 （该方法已废弃，暂时不删除）
	 * 
	 * @param PKID
	 *            String
	 * @throws SQLException
	 */
	public void deleteAll(String PKID, String defId) throws SQLException {
		if (PKID.equals("root") || PKID.equals("1000000000000"))// 系统确定的node的根。必须保护，不可以删除。
			return;
		CallableStatement proc = null;
		try {
			/**
			 * 功能改进： liulei modified （1）pro_delete_Node2(?) 新存储模式下的存储过程
			 * （2）pro_delete_node(?,?)如果在删除推荐的内容的时候，只根据nodeid会删除所有的节点,这样是不正确的。
			 * 因此，需要根据nodeid和definition两个参数来确定唯一删除的节点
			 */
			proc = connection.prepareCall("{ call pro_delete_Node_liulei(?,?)  }");
			proc.setString(1, PKID);
			proc.setString(2, defId);
			proc.execute();
		} finally {
			proc.close();
		}
	}

	/*
	private static final String SQL_REORDER = "update node set vindex=vindex-1 WHERE name=? AND parent_id=? AND vindex>? ";

	public void deleteAll(String PKID) throws SQLException {
	
		if (PKID.equals("root") || PKID.equals("1000000000000"))// 系统确定的node的根。必须保护，不可以删除。
			return;
		NodeData node = this.findByID(PKID);
		String parentID = node.getParentID();
		String name = node.getName();
		CallableStatement proc = null;
		try {
			proc = connection.prepareCall("{ call jspro_delete_node(?) }");
			proc.setString(1, PKID);
			proc.execute();
		} finally {
			proc.close();
		}

		// 根据parentID和name整理orderno的值
		PreparedStatement command = null;
		try {
			command = connection.prepareStatement(SQL_REORDER);
			command.setString(1, name);
			command.setString(2, parentID);
			command.setInt(3, node.getOrderNo());
			command.executeUpdate();
		} finally {
			this.close(command);
		}
	}
	*/
}
